﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EFIrstTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetData();
        }
        Expression<Func<teacher, bool>> strWhere = u => u.teacherno != null;//拼凑字符串需求lambda扩展类文件与ParameterRebinder.cs文件
        public void GetData()
        {
            List<teacher> teach = new BaseDAL<teacher>().GetListBy(strWhere);//查询
            GridView1.DataSource = teach;
            GridView1.DataBind();
        }

        protected void search_Click(object sender, EventArgs e)
        {
          
            string id =  teachid.Text.Trim();
            string name =  teachname.Text.Trim();
           
            if (!string.IsNullOrEmpty(id)&&!string.IsNullOrEmpty(name))
            {
                strWhere = strWhere.And(u => u.teacherno == id);
                strWhere = strWhere.And(u => u.tname == name);
                GetData();
            }
            else
            {
                Response.Write("没有数据");
            }
            
        }

        protected void delbtn_Click(object sender, EventArgs e)
        {
            string id = delid.Text.Trim();
            
                //teacher tech = new BaseDAL<teacher>().GetModel(u => u.teacherno == id);//获取实体对象,第一种方法
                //new BaseDAL<teacher>().Del(tech);
                new BaseDAL<teacher>().DelBy(u => u.teacherno == id);//注意外键关系，删除的时候不能删除外键
                GetData();
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string id = tid.Text.Trim();
            string name = tname.Text.Trim();
            string mar = major.Text.Trim();
            string pf = prof.Text.Trim();
            string depa = depart.Text.Trim();
            teacher tech = new teacher();
            tech.teacherno = id;
            tech.tname = name;
            tech.major = mar;
            tech.prof = pf;
            tech.department = depa;
            new BaseDAL<teacher>().Add(tech);
            GetData();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string id = TextBox1.Text.Trim();
            string name = TextBox2.Text.Trim();
            string mar = TextBox3.Text.Trim();
            string pf = TextBox4.Text.Trim();
            string depa = TextBox5.Text.Trim();
            teacher t = new BaseDAL<teacher>().GetModel(u => u.teacherno == id);
            t.tname = name;
            t.major = mar;
            t.prof = pf;
            t.department = depa;
            new BaseDAL<teacher>().Modify(t);
            GetData();
        }
    }
}